package spring.factorybean;

import com.sun.org.apache.bcel.internal.generic.Select;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author zhangyan
 * @version $ Id: MyFactoryBean.java, v 0.1 2020/5/14 13:37 zhangyan Exp $
 */
@Component
public class MyFactoryBean implements FactoryBean, InvocationHandler {
	private Class clazz;

	public MyFactoryBean(Class clazz) {
		this.clazz = clazz;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("proxy");
		//打印sql语句
		System.out.println("sql语句准备完毕。。。。。");
		System.out.println("执行sql查询.....");
		return null;
	}


	@Override
	public Object getObject() throws Exception {
		//返回clazz的代理对象
		return Proxy.newProxyInstance(this.getClass().getClassLoader(),
				new Class[]{clazz},
				this);
	}

	@Override
	public Class<?> getObjectType() {
		return clazz;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}
}
